﻿using System.Text;
using System.IO;
using iTunesExport.Parser;
using iTunesUtil.Properties;

namespace iTunesUtil
{
    public class ExportFileFormatter
    {
        const string sep = "-";        
        public enum FileFormat
        {
            OriginalFileName,
            GenreArtistAlbumTrack,
            ArtistAlbumTrack,
            AlbumTrack,
            Track
        };

        readonly FileFormat fileFormat = FileFormat.OriginalFileName;
        readonly bool numerate;
        readonly string expDir;
        public ExportFileFormatter(string exportDir)
        {
            expDir = exportDir;
            numerate = Settings.Default.FileNumerate;
            var format = (FileFormat) Settings.Default.FileFormat;
            switch (format)
            {
                case FileFormat.OriginalFileName: fileFormat = FileFormat.OriginalFileName; break;
                case FileFormat.GenreArtistAlbumTrack: fileFormat = FileFormat.GenreArtistAlbumTrack; break;
                case FileFormat.ArtistAlbumTrack: fileFormat = FileFormat.ArtistAlbumTrack; break;
                case FileFormat.AlbumTrack: fileFormat = FileFormat.AlbumTrack; break;
                case FileFormat.Track: fileFormat = FileFormat.Track; break;
            }
        }

        public string Generate(Track track, long trackNo)
        {                        
            var sb = new StringBuilder();
            if (numerate)
                sb.Append(string.Format("{0:D2}-", trackNo));
           
            switch (fileFormat)
            {
                case FileFormat.OriginalFileName:
                    sb.Append(Path.GetFileName(track.Location));
                    break;
                case FileFormat.GenreArtistAlbumTrack:
                    sb.Append(track.Genre).Append(sep);
                    sb.Append(track.Artist).Append(sep);
                    sb.Append(track.Album).Append(sep);
                    sb.Append(track.Name);
                    sb.Append (Path.GetExtension(track.Location));
                    break;
                case FileFormat.ArtistAlbumTrack:
                    sb.Append(track.Artist).Append(sep);
                    sb.Append(track.Album).Append(sep);
                    sb.Append(track.Name);
                    sb.Append (Path.GetExtension(track.Location));
                    break;
                case FileFormat.AlbumTrack:
                    sb.Append(track.Album).Append(sep);
                    sb.Append(track.Name);
                    sb.Append (Path.GetExtension(track.Location));
                    break;
                case FileFormat.Track:
                    sb.Append(track.Name);
                    sb.Append (Path.GetExtension(track.Location));
                    break;
            }         
            return Path.Combine(expDir, FilterInvalidPathCharacters(sb.ToString()));
        }
          
        static public string FilterInvalidPathCharacters(string fileName)
        {
            var sb = new StringBuilder();
            sb.Append(fileName);
            char[] invalidFileChars = Path.GetInvalidFileNameChars();
            foreach (char invalidFChar in invalidFileChars)
            {
                sb.Replace(invalidFChar, '.');
            }
            return sb.ToString();
        }
    }
}
